﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Common.Model;
using Common.Util;
using pinduoduo.Model;
using RestSharp;

namespace Common.PddApi
{
    public class PddPiFaApi
    {

        public static Task<IRestResponse> QueryGoodsDetail(string goodId,  string antiContent)
        {

            string url = "https://pifa.pinduoduo.com/pifa/goods/queryGoodsDetail";
            var restclient =RestClientUtil.CreateRestClient(url);

            var request = new RestRequest(Method.POST);
         
            restclient.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";

            //request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            //request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/837.36 (KHTML, like Gecko) Chrome/82.0.3202.89 Safari/537.36");
            request.AddHeader("Host", "pifa.pinduoduo.com");
            request.AddHeader("Referer", "https://pifa.pinduoduo.com/goods/detail/?gid="+ goodId);

            request.AddObject(new { goodsId = goodId });
            //var response = restclient.Execute(request);
            return restclient.ExecuteAsync(request);

        }

        public static Task<IRestResponse> SingleBuy(PddToken pddToken, CreateOrderItemListItem pddOrder, string antiContent, string daili) 
        {
            string url = "https://mms.pinduoduo.com/pifa/goods/singleBuy";

            var restclient =RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mms.pinduoduo.com");
            request.AddHeader("Referer", "https://mai.pinduoduo.com/");
            request.AddHeader("Origin", "https://mai.pinduoduo.com");
            request.AddHeader("Anti-Content", antiContent);

            PddPifaCreateOrder pddPifaCreateOrder = new PddPifaCreateOrder();
            pddPifaCreateOrder.createOrderItemList = new List<CreateOrderItemListItem>();
            pddPifaCreateOrder.createOrderItemList.Add(pddOrder);
            request.AddJsonBody(pddPifaCreateOrder);
            return restclient.ExecuteAsync(request);
        }

        public static async Task<string> GetTransacOrderCouponHtml(PddToken pddToken, string url,string daili)
        {
            var restclient =RestClientUtil.CreateRestClient(url);

            restclient.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.GET);
            //request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", url);
            request.AddCookie("pdd_user_id", pddToken.pdd_user_id);
            request.AddCookie("PDDAccessToken", pddToken.PDDAccessToken);

            var res = await restclient.ExecuteAsync(request);
            if (res.StatusCode == HttpStatusCode.OK) 
            {
                return res.Content;
            }
            return "";
        }

        public static Task<IRestResponse> PiFaOrderCreate(PddToken pddToken, PifaClientOrderCreate pifaClientOrderCreate, string daili)
        {
            string ur1 = "https://mobile.yangkeduo.com/proxy/api/api/pifa-client/order/order_create?pdduid=" + pddToken.pdd_user_id;

            var restclient = new RestClient(ur1);

            restclient.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", "https://mobile.yangkeduo.com/");
            request.AddHeader("Origin", "https://mobile.yangkeduo.com");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddCookie("pdd_user_id", pddToken.pdd_user_id);
            request.AddCookie("PDDAccessToken", pddToken.PDDAccessToken);

            request.AddJsonBody(pifaClientOrderCreate);
            return restclient.ExecuteAsync(request);
        }
            
        public static Task<IRestResponse> PiFPrepay(PddToken pddToken, string parent_order_sn, string daili) 
        {
            string ur1 = "https://mobile.yangkeduo.com/proxy/api/api/vancouver/cart/prepay?pdduid=" + pddToken.pdd_user_id;

            var restclient = new RestClient(ur1);

            restclient.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", "https://mobile.yangkeduo.com/");
            request.AddHeader("Origin", "https://mobile.yangkeduo.com");
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);

            request.AddCookie("pdd_user_id", pddToken.pdd_user_id);
            request.AddCookie("PDDAccessToken", pddToken.PDDAccessToken);

            PiFaPrePay piFaPrePay = new PiFaPrePay();
            piFaPrePay.parent_order_sn = parent_order_sn;
            piFaPrePay.return_url = "https://mobile.yangkeduo.com/transac_wappay_callback.html?order_sn="+ parent_order_sn + "&prepay_type=combine";

            request.AddJsonBody(piFaPrePay);
            return restclient.ExecuteAsync(request);
        }

        public static Task<IRestResponse> PiFaOrder(PddToken pddToken, PDDOrder pddOrder, string source, string daili) 
        {
            string ur1 = "http://apiv4.yangkeduo.com/order?pdduid=" + pddToken.pdd_user_id;
            string Referer = "http://mobile.yangkeduo.com/search_result.html?search_key=" + source + "&source=search&options=1&search_met_track=hot&refer_search_met_pos=0&refer_page_el_sn=615206&bui_search_id=" + pddOrder.group_id + "&refer_page_name=search&item_index=0&sp=0&is_back=1";
            Referer = HttpUtility.UrlEncode(Referer);

            var restclient = new RestClient(ur1);

            restclient.UserAgent = "android Mozilla/5.0 (Linux; Android 6.1; OPPO R9tm Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.121 Mobile Safari/537.36";

            restclient.Proxy = RestClientUtil.CreateWebProxy(daili);

            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json;charset=UTF-8");
            request.AddHeader("Accept", "*/*;zh-cn");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "mobile.yangkeduo.com");
            request.AddHeader("Referer", Referer);
            request.AddHeader("AccessToken", pddToken.PDDAccessToken);
            request.AddHeader("Origin", "https://mobile.yangkeduo.com");
            
            request.AddJsonBody(pddOrder);
            return restclient.ExecuteAsync(request);
        }
    }
}
